import json

import jsonpath
import requests

from Base.BaseSetting import url_path
from Base.PublicFunction import Requst


class HumanMangement():
    def __init__(self):
        self.url = url_path + '/WoniuBoss4.0'
        self.session = Requst.get_session()

    def create_staff(self,region_id,department_id,position,name,sex,entry_time,tel,work_id):
        url = self.url+'/employee/addEmp'
        headers = {'Content-Type':'text/plain;charset=UTF-8'}
        data = {f'emp.region_id':{region_id},
                'emp.department_id':{department_id},
                'emp.position':	{position},
                'emp.employee_name': {name},
                'emp.sex':{sex},
                'emp.entry_time':{entry_time},
                'emp.tel':{tel},
                'emp.email': "",
                'emp.qq':"",
                'emp.education':"",
                'emp.university':"",
                'emp.major ':"",
                'emp.address':"",
                'emp.source': "",
                'emp.cardnum ':"",
                'emp.identity':"",
                'emp.birthday ':"",
                'emp.birthday_type ':"01",
                'emp.emergency_contact ':"",
                'emp.emergency_tel ':"",
                'emp.emegency_relation ':"",
                'emp.work_id':{work_id}
        }

        resobj = self.session.post(url=url,data=data,headers=headers)
        content = resobj.text
        status_code = resobj.status_code
        res = resobj.content
        return content,status_code,res

    def query_staff(self,name):
        url = self.url+'/employee/queryEmpList'
        headers = {'Content-Type':'application/x-www-form-urlencoded'}

        data = f'pageSize=10&pageIndex=1&regionId=1&deptId=2&empName={name}&status=01'
        resobj  = self.session.post(url=url,data=data.encode('utf-8'),headers=headers)
        content  =resobj.text
        info = json.loads(content)
        query_info  = jsonpath.jsonpath(info,'$..employee_name')
        status_code = resobj.status_code
        res = resobj.content

        return query_info,status_code,res

class  Administration(): #行政综合
    def __init__(self):
        self.url = url_path + '/WoniuBoss4.0'
        self.session = Requst.get_session()

    def purchase(self,name,type,barcode,price,time,owner):
        url = self.url+'/assets/addAss'
        data = {
                f'ass.assets_name':{name},
                'ass.assets_type':	{type},
                'ass.bar_code':	{barcode},
                'ass.price':{price},
                'purchase_employee':"超级管理员",
                'ass.purchase_employee_id':	"1",
                'ass.purchase_time':{time},
                'ass.note':	"",
                'ass.assets_owner':{owner}
        }
        resobj  = self.session.post(url=url,data=data)
        content = resobj.text
        status_code = resobj.status_code
        res = resobj.content
        return content,status_code,res

    def queryinfo(self):
        url = self.url + '/assets/queryAssByInfo'
        headers = {'Content-Type':'application/x-www-form-urlencoded'}
        data = 'pageSize=10&pageIndex=1&regionId=1&type=&assOwner=&barCode=&count=&state='
        resobj = self.session.post(url=url,data=data,headers=headers)
        content = resobj.text
        info = json.loads(content)
        actual_res = jsonpath.jsonpath(info,'$..region_name')
        status_code = resobj.status_code
        res = resobj.content

        return actual_res,status_code,res


class System_Management():
    def __init__(self):
        self.url = url_path + '/WoniuBoss4.0'
        self.session = Requst.get_session()


    def modifyinfo(self,name):
        url = self.url+'/user/queryUser'
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        data = 'pageSize=10&pageIndex=1&userName=&empName={}'.format(name)
        resobj = self.session.post(url=url,headers=headers,data=data.encode('utf-8'))

        content = resobj.text
        info = json.loads(content)
        res = jsonpath.jsonpath(info,'$..id')
        status_code = resobj.status_code
        res1 = resobj.content

        return res[1],status_code, res1

    def modifysave(self,userid,des):
        url = self.url+'/user/saveSetUser'
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        data={
                f'user.id':{userid},
                'user.name':"WNCD100",
                'user.status':"01",
                'user.des':{des}

        }
        resobj = self.session.post(url=url,data=data,headers=headers)
        content=resobj.text
        status_code = resobj.status_code
        res= resobj.content

        return content,status_code,res


if __name__ == '__main__':
    hm = HumanMangement()
    # hm.create_staff()
    a = hm.query_staff('张丹')









